﻿# -*- encoding: utf-8 -*- 



from datetime import datetime, timedelta
from openerp import SUPERUSER_ID
from openerp import api, fields, models, _
import openerp.addons.decimal_precision as dp
from openerp.exceptions import UserError
from openerp.tools import float_is_zero, float_compare, DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools import partition, pycompat, collections
class fhgroups_fh_role(models.Model):
    _name = 'fhgroups.fh_role'
    _description = u'角色管理'
    _rec_name='fhname'
    def get_employee(self):
        obj = self.env['res.groups']
        idsobj = obj.search([("fclass_name","!=","")],order="fclass_name")
        print('-------------------------------------')		
        print(self.env.user.id)		
        #print(ids)	
        res =[]	
		
        fclass_name=""		
        Fr = idsobj.read()
        for item in Fr:	
            print(item)
            if(fclass_name==item["fclass_name"]):			
                fclass_name= ""
            else:			
                fclass_name= item["fclass_name"]				
            res.append((0, 0, {'fhgb': False, 'groups_id': item["id"], 'fclass_name': fclass_name, 'name': item["name"]}))		
        #res.append((0, 0, {'fhgb': True, 'groups_id': 16, 'fclass_name': '111', 'name': '12222'}))		
        print(res)					
        return res
			
	#_default_line_ids=[ (0, 0,{ values })]
	#default=	
    fhname=fields.Char(u'角色',readonly=False,required=False,translate=False)
    users = fields.Many2many('res.users', 'fhres_role_users_rel', 'gid', 'uid')  	
    groups = fields.Many2many('res.groups', 'fhres_role_groups2_rel', 'gid', 'uid')
    groups2 = fields.Many2many('res.groups', 'fhres_role_groups3_rel', 'gid', 'uid')   	
    #groups_id = fields.Many2many('res.groups', 'res_role_groups3_rel', 'gid', 'uid') 
    #order_line = fields.One2many('fhgroups.fh_roleline', 'pay_id', string='Order Lines', copy=True,default=lambda self: self.get_employee())
    order_line = fields.One2many('fhgroups.fh_roleline', 'pay_id', string='Order Lines', copy=True)
		
    @api.model		
    def create(self, vals):
		
        result = super(fhgroups_fh_role, self).create(vals)	
        return result
        
    @api.multi		
    def fhwritev1(self):	
        print('self.groups.ids',self.groups.ids)
        if(self.groups!=None):
            self_write=self.env['res.groups'].search([('id','in',self.groups.ids)])
            FHLIST1=[]
            for itme in  self_write:
                print('itme.users.ids',itme.users.ids)
                print('self.users.ids',self.users.ids)
                FHLIST1=itme.users.ids                
                for item2 in itme.users.ids:
                    for item3 in self.users.ids:  
                        if(item2==item3):
                            print('OK1',item2)                         
                            FHLIST1.remove(item2)
                            print('OK2',FHLIST1)                            
                print('FHLIST1',FHLIST1)                            
                itme.write({'users':[[6, False, FHLIST1]]})
		
        return True  

    @api.multi		
    def fhwritev3(self):	
        print('self.groups2.ids',self.groups2.ids)
        if(self.groups2!=None):
            self_write=self.env['res.groups'].search([('id','in',self.groups2.ids)])
            FHLIST1=[]
            for itme in  self_write:
                print('itme.users.ids',itme.users.ids)
                print('self.users.ids',self.users.ids)
                FHLIST1=itme.users.ids                
                for item2 in itme.users.ids:
                    for item3 in self.users.ids:  
                        if(item2==item3):
                            print('OK1',item2)                         
                            FHLIST1.remove(item2)
                            print('OK2',FHLIST1)                            
                print('FHLIST1',FHLIST1)                            
                itme.write({'users':[[6, False, FHLIST1]]})

            FHLIST2=self.groups.ids
            for item1 in  self.groups.ids:
                for item2 in  self.groups2.ids:            
                    if(item1==item2):
                        FHLIST2.remove(item1)
            self.write({'groups':[[6, False, FHLIST2]]})                        
        return True 


        
    @api.multi		
    def fhwritev2(self):	
        print('self.groups.ids',self.groups.ids)
        if(self.groups!=None):
            self_write=self.env['res.groups'].search([('id','in',self.groups.ids)])
            for itme in  self_write:
                print('itme ',itme.id)  			
                itme.write({'users':[[6, False, itme.users.ids+self.users.ids]]})
		
        return True         
        
    @api.multi		
    def write(self, values):	

        result = super(fhgroups_fh_role, self).write(values)		
        return result
    @api.multi
    def unlink(self):
        for itme in  self:        
            self_write=self.env['res.groups'].search([('id','in',itme.groups.ids)])
            for itme2 in  self_write:
                itme2.write({'users':[[6, False, []]]})	
	
        return super(fhgroups_fh_role, self).unlink()
    @api.multi
    def copy(self, default=None):
        return super(fhgroups_fh_role, self).copy(default)
    @api.multi
    def search(self, args, offset=0, limit=None, order=None, context=None, count=False):
        domain =None		
        domain = [('state', '=', '0')]		
        return super(fhgroups_fh_role, self).search(args, offset=offset, limit=limit, order=order, count=count)		
fhgroups_fh_role() #对象定义结束
class fhgroups_fh_roleline(models.Model):
    _name = 'fhgroups.fh_roleline'
    _description = u'详细信息'
    #_rec_name='FClass'
    def get_employee(self, cr, uid, context={}):
        obj = self.pool.get('hr.employee')
        ids = obj.search(cr, uid, [('user_id','=',uid)])
        res = obj.read(cr, uid, ids, ['id','name'], context)
        return res and res[0]['id'] or 0

    pay_id=fields.Many2one('fhgroups.fh_role',u'fhgroups_fh_role',required=True, ondelete='cascade', index=True, copy=False)	
    groups_id=fields.Many2one('res.groups',u'groups',)	
    fclass_name = fields.Char(string=u'群组分类')	
    fhgb = fields.Boolean(string=u'选择',)	
    name = fields.Char(required=True,string=u'名称',)		
    fkec=fields.Char(u'课程',readonly=False,required=False,translate=False)
    fkecsl=fields.Integer(u'课时',readonly=False,required=False,translate=False)
    FAddperson =fields.Many2one('hr.employee',u'添加人',readonly=False,required=False,translate=False)
    FConsultant=fields.Many2one('hr.employee',u'班主任',readonly=False,required=False,translate=False)					   
    FAdddate= fields.Datetime(u'添加日期',readonly=False,required=False,translate=False)
    state=fields.Selection([('0',u'未分班'),('1',u'已分班')],u'是否分班',readonly=False,required=False,translate=False)					   
    FDisable=fields.Selection([('0',u'启用'),('1',u'停用')],u'是否停用',readonly=False,required=False,translate=False) 

    @api.multi
    def search(self, args, offset=0, limit=None, order=None, context=None, count=False):
        domain =None		
        domain = [('state', '=', '0')]
        #context={'group_by':'fclass_name'}
		
        return super(fhgroups_fh_roleline, self).search(args, offset=offset, limit=limit, order=order, count=count)	
	
fhgroups_fh_roleline() #对象定义结束